Imports MIMS.WRS.Business

Public Class ChangePassword
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblComment As System.Web.UI.WebControls.Label
    Protected WithEvents txtOldPassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtNewPassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtRetypePassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnSave As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region


#Region " Properties"

    Protected ReadOnly Property userID() As Integer
        Get
            If Not Session("CurrentUserID") Is Nothing Then
                Return CInt(Session("CurrentUserID"))
            Else
                Return 0
            End If
        End Get
    End Property

    Protected Property OldPass() As String
        Get
            If Not Session("CurrentOldPass") Is Nothing Then
                Return CStr(Session("CurrentOldPass"))
            Else
                Return ""
            End If
        End Get
        Set(ByVal Value As String)
            Session("CurrentOldPass") = Value
        End Set
    End Property

    Private Property Url() As String
        Get
            Return CStr(ViewState("Url"))
        End Get
        Set(ByVal Value As String)
            ViewState("Url") = Value
        End Set
    End Property

    Private Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        MIMS_WRS.Web.Security.AdminOnly()

        If Not Me.IsPostBack Then
            Me.Url = Request.Url.PathAndQuery
            Me.UrlReferrer = Request.UrlReferrer.PathAndQuery
            If Me.userID <> 0 Then
                Me.OldPass = USERService.GetOldPassword(Me.userID)
            End If
        End If
    End Sub

    Private Sub UpdatePassword()
        Try
            USERService.UpdatePassword(Me.userID, Utilities.HashPassword(txtNewPassword.Text))
        Catch ex As Exception
            Throw New ApplicationException(ex.Message)
        End Try
    End Sub

    Private Overloads Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If (Utilities.HashPassword(txtOldPassword.Text) <> Me.OldPass) Then
                lblErr.Text = "Old Password is not valid !"
            Else
                UpdatePassword()
                lblComment.Text = "Your Password has been changed successfully!"
                Response.Redirect(Me.UrlReferrer)
            End If
        Catch ex As Exception
            lblErr.Text = ex.Message
        End Try
    End Sub

    Private Overloads Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(Me.UrlReferrer)
    End Sub
End Class
